| 1 | package net.mandaria.tippytipper.preferences; |
| 2 | |
| 3 | import net.mandaria.tippytipper.widgets.*; |
| 4 | import android.content.Context; |
| 5 | import android.util.AttributeSet; |
| 6 | import android.view.Gravity; |
| 7 | import android.view.View; |
| 8 | import android.preference.DialogPreference; |
| 9 | import android.widget.TableLayout; |
| 10 | import android.widget.TableRow; |
| 11 | import android.widget.TextView; |
| 12 | |
| 13 | public class DecimalPreference extends DialogPreference |
| 14 | { |
| 15 | private static final String androidns = "http://schemas.android.com/apk/res/android"; |
| 16 | private NumberPicker mPickInteger, mPickDecimal; |
| 17 | private TextView mSplashText; |
| 18 | private Context mContext; |
| 19 | |
| 20 | private String mDialogMessage; |
| 21 | private float mDefault, mValue = 0; |
| 22 | private int mInteger, mDecimal = 0; |
| 23 | |
| 24 | public DecimalPreference(Context context, AttributeSet attrs) |
| 25 | { |
| 26 | super(context, attrs); |
| 27 | mContext = context; |
| 28 | |
| 29 | mDialogMessage = attrs.getAttributeValue(androidns, "dialogMessage"); |
| 30 | attrs.getAttributeValue(androidns, "text"); |
| 31 | mDefault = attrs.getAttributeIntValue(androidns, "defaultValue", 0); |
| 32 | } |
| 33 | |
| 34 | @Override |
| 35 | protected View onCreateDialogView() |
| 36 | { |
| 37 | // LinearLayout layout = new LinearLayout(mContext); |
| 38 | TableLayout layout = new TableLayout(mContext); |
| 39 | // layout.setOrientation(LinearLayout.VERTICAL); |
| 40 | layout.setPadding(6, 6, 6, 6); |
| 41 | |
| 42 | mSplashText = new TextView(mContext); |
| 43 | if (mDialogMessage != null) |
| 44 | mSplashText.setText(mDialogMessage); |
| 45 | |
| 46 | TableRow row_header = new TableRow(mContext); |
| 47 | row_header.addView(mSplashText); |
| 48 | |
| 49 | mPickInteger = new NumberPicker(mContext); |
| 50 | mPickDecimal = new NumberPicker(mContext); |
| 51 | mPickDecimal.setFormatter(NumberPicker.THREE_DIGIT_FORMATTER); |
| 52 | |
| 53 | TextView dot = new TextView(mContext); |
| 54 | dot.setText("."); |
| 55 | dot.setTextSize(32); |
| 56 | |
| 57 | TextView percent = new TextView(mContext); |
| 58 | percent.setText("%"); |
| 59 | percent.setTextSize(32); |
| 60 | |
| 61 | TableRow row_one = new TableRow(mContext); |
| 62 | row_one.setGravity(Gravity.CENTER); |
| 63 | row_one.addView(mPickInteger); |
| 64 | row_one.addView(dot); |
| 65 | row_one.addView(mPickDecimal); |
| 66 | row_one.addView(percent); |
| 67 | |
| 68 | layout.addView(row_header); |
| 69 | |
| 70 | TableLayout table_main = new TableLayout(mContext); |
| 71 | table_main.addView(row_one); |
| 72 | |
| 73 | TableRow row_main = new TableRow(mContext); |
| 74 | row_main.setGravity(Gravity.CENTER_HORIZONTAL); |
| 75 | row_main.addView(table_main); |
| 76 | |
| 77 | layout.addView(row_main); |
| 78 | |
| 79 | if (shouldPersist()) |
| 80 | mValue = getPersistedFloat(mDefault); |
| 81 | |
| 82 | bindData(); |
| 83 | |
| 84 | return layout; |
| 85 | } |
| 86 | |
| 87 | private void bindData() |
| 88 | { |
| 89 | mInteger = (int) Math.floor(mValue); |
| 90 | float decimal = (mValue * 1000) - (mInteger * 1000); |
| 91 | mDecimal = (int) decimal; |
| 92 | try |
| 93 | { |
| 94 | mPickInteger.setCurrent(mInteger); |
| 95 | mPickDecimal.setCurrent(mDecimal); |
| 96 | } |
| 97 | catch (Exception ex) |
| 98 | { |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | protected void onBindDialogView(View v) |
| 104 | { |
| 105 | super.onBindDialogView(v); |
| 106 | bindData(); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | protected void onSetInitialValue(boolean restore, Object defaultValue) |
| 111 | { |
| 112 | super.onSetInitialValue(restore, defaultValue); |
| 113 | if (restore) |
| 114 | { |
| 115 | try |
| 116 | { |
| 117 | mValue = shouldPersist() ? getPersistedFloat(mDefault) : 0; |
| 118 | } |
| 119 | catch (Exception ex) |
| 120 | { |
| 121 | mValue = mDefault; |
| 122 | } |
| 123 | } |
| 124 | else |
| 125 | mValue = (Float) defaultValue; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | protected void onDialogClosed(boolean positiveResult) |
| 130 | { |
| 131 | if (positiveResult == true) |
| 132 | { |
| 133 | super.onDialogClosed(positiveResult); |
| 134 | // HACK: "click" both picker inputs to validate inputs before closing the dialog |
| 135 | // this is to fix a problem of closing the dialog not causing the onFocusChange of the picker |
| 136 | // to be called |
| 137 | mPickInteger.onClick(null); |
| 138 | mPickDecimal.onClick(null); |
| 139 | String value = mPickInteger.getCurrent() + "." + mPickDecimal.getCurrentFormatted(); |
| 140 | mValue = Float.valueOf(value); |
| 141 | if (shouldPersist()) |
| 142 | persistFloat(mValue); |
| 143 | } |
| 144 | } |
| 145 | } |